package com.sunwayworld.basemodule.rm.train.question.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.sunwayworld.basemodule.rm.train.question.bean.LimsCoreQuestionBean;
import com.sunwayworld.basemodule.rm.train.question.bean.LimsCoreQuestionOptBean;
import com.sunwayworld.basemodule.rm.train.question.dao.LimsCoreQuestionOptDao;
import com.sunwayworld.basemodule.rm.train.question.service.LimsCoreQuestionOptService;
import com.sunwayworld.basemodule.rm.train.question.service.LimsCoreQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.audit.aunnotation.Audit;
import com.sunwayworld.framework.audit.constant.AuditConstant;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.context.LocalContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.support.PersistableMetadataHelper;
import com.sunwayworld.module.mdm.user.bean.CoreUserBean;
import com.sunwayworld.module.sys.metadata.service.CoreTableService;

@GikamBean
@Repository
public class LimsCoreQuestionOptServiceImpl implements LimsCoreQuestionOptService {
    @Autowired
    private LimsCoreQuestionOptDao limsCoreQuestionOptDao;

    @Autowired
    @Lazy
    private LimsCoreQuestionService limsCoreQuestionService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsCoreQuestionOptDao getDao() {
        return limsCoreQuestionOptDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    @Audit(AuditConstant.INSERT)
    public Long insert(RestJsonWrapperBean wrapper) {
        LimsCoreQuestionOptBean questionOpt = wrapper.parseUnique(LimsCoreQuestionOptBean.class);
        questionOpt.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(questionOpt);

        CoreUserBean currentUser = LocalContextHelper.getLoginUser();
        LimsCoreQuestionBean question4Update = new LimsCoreQuestionBean();
        question4Update.setId(questionOpt.getQuestionId());
        question4Update.setLastUpdatedById(currentUser.getId());
        question4Update.setLastUpdatedByName(currentUser.getUserName());
        question4Update.setLastUpdatedTime(LocalDateTime.now());

        limsCoreQuestionService.getDao().update(question4Update, "LASTUPDATEDBYID", "LASTUPDATEDBYNAME", "LASTUPDATEDTIME");
        return questionOpt.getId();
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.DELETE)
    @Audit(AuditConstant.DELETE)
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsCoreQuestionOptBean> deleteList = wrapper.parse(this.getDao().getType());
        if (deleteList.isEmpty()) {
            return;
        }

        List<Long> questionOptIdList = deleteList.stream().map(LimsCoreQuestionOptBean::getId).collect(Collectors.toList());
        List<LimsCoreQuestionOptBean> questionOptList = getDao().selectListByIds(questionOptIdList);
        List<Long> questionIdList = questionOptList.stream().map(LimsCoreQuestionOptBean::getQuestionId).distinct().collect(Collectors.toList());

        CoreUserBean currentUser = LocalContextHelper.getLoginUser();
        LocalDateTime currentTime = LocalDateTime.now();

        List<LimsCoreQuestionBean> questionList4Update = new ArrayList<>();
        questionIdList.forEach(e -> {
            LimsCoreQuestionBean question = new LimsCoreQuestionBean();
            question.setId(e);
            question.setLastUpdatedById(currentUser.getId());
            question.setLastUpdatedByName(currentUser.getUserName());
            question.setLastUpdatedTime(currentTime);
            questionList4Update.add(question);
        });

        limsCoreQuestionService.getDao().update(questionList4Update, "LASTUPDATEDBYID", "LASTUPDATEDBYNAME", "LASTUPDATEDTIME");

        CoreTableService tableService = ApplicationContextHelper.getBean(CoreTableService.class);
        deleteList.forEach(t -> tableService.deleteCascade(PersistableMetadataHelper.getTableName(getDao().getType()), t.getId()));

    }
}